package com.taskcollection.control;

import com.alibaba.fastjson.JSONObject;
import com.taskcollection.bean.StudentBean;
import com.taskcollection.bean.TeacherBean;
import com.taskcollection.service.StudentService;
import com.taskcollection.service.TeacherService;
import com.taskcollection.serviceInterface.TeacherServiceInterface;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2016/8/6 0006.
 */
public class LoginControl {
    private TeacherServiceInterface ts;

    public TeacherServiceInterface getTs() {
        return ts;
    }

    public void setTs(TeacherServiceInterface ts) {
        this.ts = ts;
    }


    public void checkLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String phone = request.getParameter("phone");
        String passwd = request.getParameter("passwd");
        String status = request.getParameter("status");
        JSONObject json = new JSONObject();
        if("教师".equals(status)){
            json.put("msg",ts.checkLogin(phone,passwd));
        }
        if("学生".equals(status)){
            StudentService ss = new StudentService();
            json.put("msg",ss.checkLogin(phone,passwd));

        }

        response.getWriter().print(json.toJSONString());
    }
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String phone = request.getParameter("phone");
        String passwd = request.getParameter("passwd");
        String status = request.getParameter("status");
        if("教师".equals(status)){
            TeacherBean tea = ts.login(phone,passwd);
            request.getSession().setAttribute("教师",tea);
            request.getRequestDispatcher("/teacher/homepage.jsp").forward(request,response);
        }
        if("学生".equals(status)){
            StudentService ss = new StudentService();
            StudentBean stu = ss.login(phone,passwd);
            request.getSession().setAttribute("学生",stu);
            request.getRequestDispatcher("/student/homepage.jsp").forward(request,response);
        }
    }
}
